<?php

class ServiceActivityModel {

  public function __construct() {
    $this->activityTable = $GLOBALS['ecs']->table('service_activity');
  }

  public function getActivityList($limit = 15, $offset = 0, $sortBy = 'topic_id', $sort_order = 'DESC', $keyword = '') {

    $sql = "SELECT * FROM $this->activityTable ORDER BY $sortBy $sort_order";
    $query = $GLOBALS['db']->selectLimit($sql, $limit, $offset);

    $res = array();
    while ($topic = $GLOBALS['db']->fetch_array($query)) {
      $topic['start_time'] = local_date('Y-m-d', $topic['start_time']);
      $topic['end_time'] = local_date('Y-m-d', $topic['end_time']);
      $topic['url'] = $GLOBALS['ecs']->url() . 'service_activity.php?topic_id=' . $topic['topic_id'];
      $res[] = $topic;
    }
    return $res;
  }

  /**
   * 列表链接
   * @param   bool    $is_add     是否添加（插入）
   * @param   string  $text       文字
   * @return  array('href' => $href, 'text' => $text)
   */
  public function list_link($is_add = true, $text = '') {
    $href = 'service_activity.php?act=list';
    if (!$is_add) {
      $href .= '&' . list_link_postfix();
    }
    if ($text == '') {
      $text = '服务点活动表表';
    }

    return array('href' => $href, 'text' => $text);
  }

  public function getCountActivityList() {
    return $GLOBALS['db']->getOne("SELECT COUNT(*) FROM $this->activityTable");
  }

}